package org.dfl.plugin.dialogs;

import java.util.Set;

import org.apache.log4j.Logger;
import org.dfl.messages.Messages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * basic abstract dialog
 * @author Piotr Wlodarczyk
 *
 */
public abstract class BaseDialog extends org.eclipse.swt.widgets.Dialog{
	private static Logger logger = Logger.getLogger(BaseDialog.class);
	
	public BaseDialog(){
		super(new Shell(Display.getDefault()),SWT.NULL);
	}
	
	public BaseDialog(Shell arg0) {
		super(arg0);
	}
	
	public BaseDialog(Shell arg0, int arg1) {
		super(arg0, arg1);
	}

	public static void placeDialogInCenter(Shell shell){
		Composite parent = Display.getDefault().getActiveShell();
		if(parent == null){
			return;
		}
		Rectangle parentSize = parent.getBounds();
		Rectangle mySize = shell.getBounds();

		int locationX, locationY;
		locationX = (parentSize.width - mySize.width)/2+parentSize.x;
		locationY = (parentSize.height - mySize.height)/2+parentSize.y;

		shell.setLocation(new Point(locationX, locationY));
	}

	public void open() {
		try {
			Shell dialogShell = getNewShell();
			
			init(dialogShell);
			dialogShell.pack();
			
			placeDialogInCenter(dialogShell);
			dialogShell.open();
			Display display = dialogShell.getDisplay();
			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}
	protected abstract void init(Shell dialogShell);
	protected abstract Shell getNewShell();
	
	protected final Text addStringRow(Shell parent, boolean textarea, boolean readonly, String labelStr, String valueStr) {
		Composite container = new Composite(parent, SWT.NONE);
		RowLayout layout = new RowLayout(org.eclipse.swt.SWT.HORIZONTAL);
		container.setLayoutData(new GridData());
		container.setLayout(layout);
		{
			Label label = new Label(container, SWT.NONE);
			RowData nameTextLData = new RowData();
			nameTextLData.width = 150;
			nameTextLData.height = 20;
			label.setLayoutData(nameTextLData);
			label.setText(labelStr+":  "); //$NON-NLS-1$
		}
		// OBSLUGA TYPU STRING
		int type = (textarea ? (SWT.MULTI | SWT.WRAP) : SWT.NONE);
		if(!readonly) type = (type | SWT.BORDER);
		final Text text = new Text(container,type);
		text.setText(valueStr==null?"":valueStr); //$NON-NLS-1$
			
		RowData nameTextLData = new RowData();
		nameTextLData.width = 350;
		nameTextLData.height = textarea ? 100:20;
		text.setLayoutData(nameTextLData);
		text.setEditable(!readonly);

		return text;
	}
	
	/**
	 * 
	 * @param parent
	 * @param textarea
	 * @param readonly
	 * @param labelStr
	 * @param valueStr
	 * @return
	 */
	protected final Text addDirSelectionRow(final Shell parent, boolean textarea, boolean readonly, String labelStr, String valueStr) {
		Composite container = new Composite(parent, SWT.NONE);
		RowLayout layout = new RowLayout(org.eclipse.swt.SWT.HORIZONTAL);
		container.setLayoutData(new GridData());
		container.setLayout(layout);
		{
			Label label = new Label(container, SWT.NONE);
			RowData nameTextLData = new RowData();
			nameTextLData.width = 150;
			nameTextLData.height = 20;
			label.setLayoutData(nameTextLData);
			label.setText(labelStr+":  "); //$NON-NLS-1$
		}
		// OBSLUGA TYPU STRING
		final Text text = new Text(container,textarea ? (SWT.MULTI | SWT.WRAP | SWT.BORDER) : SWT.NONE | SWT.BORDER);
		text.setText(valueStr==null?"":valueStr); //$NON-NLS-1$
			
		RowData nameTextLData = new RowData();
		nameTextLData.width = 250;
		nameTextLData.height = textarea ? 100:20;
		text.setLayoutData(nameTextLData);
		text.setEditable(!readonly);
		
		Button dirChooserButton = new Button(container, SWT.PUSH	| SWT.CENTER);
			RowData dirChooserButtonLData = new RowData();
			dirChooserButtonLData.width = 90;
			dirChooserButtonLData.height = 23;
			dirChooserButton.setLayoutData(dirChooserButtonLData);
			dirChooserButton.setText(Messages.TransitionPropertiesDialog_select);
			dirChooserButton.addListener(SWT.Selection,new Listener(){
				public void handleEvent(Event arg0) {
					DirectoryDialog dlg = new DirectoryDialog(parent, SWT.SINGLE);
			        
			        // Change the title bar text
			        dlg.setText(Messages.TransitionPropertiesDialog_selectStartDir);

			        // null if user cancels
			        String dir = dlg.open();
			        if (dir != null) {
			        	// Set the text box to the new selection
			        	text.setText(dir);
			        }
				}});
		dirChooserButton.setEnabled(!readonly);
		return text;
	}
	

	/**
	 * add combo box row
	 * @param parent
	 * @param editable
	 * @param readonly
	 * @param labelStr
	 * @param values
	 * @param selected
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected final Combo addComboRow(Shell parent, boolean editable, boolean readonly, String labelStr, Set values, Object selected) {
		Composite container = new Composite(parent, SWT.NONE);
		RowLayout layout = new RowLayout(org.eclipse.swt.SWT.HORIZONTAL);
		container.setLayoutData(new GridData());
		container.setLayout(layout);
		{
			Label label = new Label(container, SWT.NONE);
			RowData nameTextLData = new RowData();
			nameTextLData.width = 150;
			nameTextLData.height = 20;
			label.setLayoutData(nameTextLData);
			label.setText(labelStr+":  "); //$NON-NLS-1$
		}
		// OBSLUGA TYPU STRING
		Combo combo = new Combo(container, editable?SWT.DROP_DOWN:SWT.READ_ONLY);
		RowData comboLData = new RowData();
		comboLData.height = 21;
		comboLData.width= 340;
		combo.setLayoutData(comboLData);
		combo.setEnabled(!readonly);
		
		if(values!=null){
			for (Object key : values) {
				combo.add(key.toString());
			}
		}
		if(selected!=null){
			combo.setText(selected.toString());
		}
		return combo;
	}
}
